// pages/detail/index.js
const api = require('../../utils/api')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        obj:{},
        user:"",
        shopObj:{

        },
        phone:"",
        show:false,
        show2:false,
        showShare:false,
        options: [
            { name: '微信', icon: 'wechat', openType: 'share' },
            // { name: '复制链接', icon: 'link' },
            // { name: '分享海报', icon: 'poster' },
            // { name: '二维码', icon: 'qrcode' },
          ],
        id:wx.getStorageSync('id'),
        userid:null,
        phone:null,
        swiperList:[],
        isCollection:false,
        recommendList:[],
        collectionId:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
            let uid = options.uid || wx.getStorageSync('uid')
            // 业务员分享分享进入
            let obj = {
                opcode:"Int16Array",
                id: parseInt(uid)|| 9
            }
            api.userinfo({
                data:obj,
                method:'post',
                success: (res)=>{
                   that.setData({
                        'shopObj.logo':res.data.data.data[0].imgpath,
                        'shopObj.name':res.data.data.data[0].name,
                        'shopObj.phone':res.data.data.data[0].phone,
                    })
                }
            })

            that.getCollection(options.id)
        api.productinfo({
            id:options.id,
            method:"get",
            success: function(res){
                that.setData({
                    obj:res.data.data.data.data,
                    recommendList:res.data.data.data.relevant.map(item=>{
                        return JSON.parse(item)
                    }),
                    swiperList:res.data.data.data.data.pic.split(";").map((item) => {
                        return {
                          name: item,
                          url: item,
                        };
                      })
                })
                if(wx.getStorageSync('id') && wx.getStorageSync('phone')){
                    that.addView()
                }
            }
        })

        that.setData({
            userid:options.userid,
            phone:options.phone
        })
    },
    call() { // 打电话
        let phone = this.data.phone || "02867871188"   // 仅为示例，并非真实的电话号码
        wx.makePhoneCall({
          phoneNumber: phone  
        })
    },
    onClick(event) {
        this.setData({ showShare: true });
    },
    gotoRelease(){
        // let phone = this.data.phone ||   "02867871188"
        // wx.makePhoneCall({
        //     phoneNumber: phone  
        // })
        this.setData({
            show:true
        })
    },
    gotoRelease2(){
        // let phone = this.data.phone ||   "02867871188"
        // wx.makePhoneCall({
        //     phoneNumber: phone  
        // })
        this.setData({
            show2:true
        })
    },
    onClose2() {
        this.setData({ show: false });
      },
        onClose3() {
        this.setData({ show2: false });
      },
    onGetUserInfo(e) {
        console.log(e.detail);
    },
    onClose() {
        this.setData({ showShare: false });
    },
    onSelect(event) {
        // Toast(event.detail.name);
        console.log(event.detail.name)
        if(event.detail.name=='复制链接'){
            wx.downloadFile({
                url: 'https://res.wx.qq.com/wxdoc/dist/assets/img/demo.ef5c5bef.jpg',
                success: (res) => {
                  wx.showShareImageMenu({
                    path: res.tempFilePath
                  })
                }
              })
        }
        // this.onClose();
    },
    handleSub1(){
        wx.requestSubscribeMessage({
            tmplIds: ['juSA0zkCPFb7-4nSyQgfSae_bQNBfWA9HULgIExMIqQ'],
            success (res) {
                console.log(res)
             }
          })
    },
    saveImage(e){
        let url = e.currentTarget.dataset.url;
        //用户需要授权
        wx.getSetting({
         success: (res) => {
          if (!res.authSetting['scope.writePhotosAlbum']) {
           wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success:()=> {
             // 同意授权
             this.saveImg1(url);
            },
            fail: (res) =>{
             console.log(res);
            }
           })
          }else{
           // 已经授权了
           this.saveImg1(url);
          }
         },
         fail: (res) =>{
          console.log(res);
         }
        })  
       },
       saveImg1(url){
        wx.getImageInfo({
         src: url,
         success:(res)=> {
          let path = res.path;
          wx.saveImageToPhotosAlbum({
           filePath:path,
           success:(res)=> { 
            wx.showToast({
              title: '保存成功,请在相册查看',
            })
           },
           fail:(res)=>{
            console.log(res);
           }
          })
         },
         fail:(res)=> {
          console.log(res);
         }
        })
       },
    
    getCollection(id){
        let that = this
        let clientid = wx.getStorageSync('id')
        if(clientid && id){
            let obj = {
                clientid:wx.getStorageSync('id'),
                productid: Number(id) 
            }
            api.collectionexist({
                data:obj,
                method:'post',
                success: function(res){
                   if(res.data.code==60013){
                    that.setData({
                        isCollection:true,
                        collectionId:res.data.data.data.id
                    })
                   }
                   if(res.data.code==60012){
                    that.setData({
                        isCollection:false,
                        collectionId:null
                    })
                   }
                }
            })
        }else{

        }
        
    },
    addView(){
        let viewList = wx.getStorageSync('viewList')
        let obj = this.data.obj
        if(viewList.length==0){
            viewList.push(obj)
            wx.setStorageSync('viewList', viewList)
        }else{
            let index = viewList.find(item=>{
                return obj.id==item.id  
            }
            )
            if(index){
                // 不做任何操作
            }else{
                viewList.push(obj)
                wx.setStorageSync('viewList', viewList)
            }
        }
       
    },
    onShareAppMessage: function(){
            let userid = wx.getStorageSync('id')
            let phone = wx.getStorageSync('phone')
          return {
            title: this.data.obj.name,
            path: 'pages/detail/index?userid='+userid+'&phone='+phone+'&id='+this.data.obj.id
          }
    },
    handleOrder(){
        // 未登陆
        // 登陆 下单
        if(!wx.getStorageSync('id') && !wx.getStorageSync('phone')){
            wx.showModal({
                title: '未授权',
                content: '您还未授权登陆，登陆后享更多服务',
                cancelText: '再看看',
                confirmText: '授权登陆',
                success: res => {
                if (res.confirm) {
                    wx.navigateTo({
                    url: '/pages/login/index'
                    });
                }
                }
            })
            return
        }
            // 切换到登录页面
       
        let clientid = wx.getStorageSync('id')
        let obj = {
            opcode: "Int16Array", 
            data: {
                productid: this.data.obj.id, 
                clientid: clientid, 
                phone: wx.getStorageSync('phone'), 
                userid: this.data.userid 
            } 
            }
            api.orderadd({
                data:obj,
                method:'post',
                success: function(res){
                   console.log(res)
                   if(res.data.success){
                       wx.showToast({
                         title: '预约成功',
                       })
                   }else{
                    wx.showToast({
                        title: '订单重复',
                      })
                   }
                }
            })
    },
    handleCartAdd(){
        if(!wx.getStorageSync('id') && !wx.getStorageSync('phone')){
            wx.showModal({
                title: '未授权',
                content: '您还未授权登陆，登陆后享更多服务',
                cancelText: '再看看',
                confirmText: '授权登陆',
                success: res => {
                if (res.confirm) {
                    wx.navigateTo({
                    url: '/pages/login/index'
                    });
                }
                }
            })
            return
        }
        let clientid = wx.getStorageSync('id')
        let that = this
        let obj = {
            opcode: "Int16Array", 
            data:{
                productid: that.data.obj.id, 
                clientid: clientid,
           }
        }
        api.collectionadd({
            data:obj,
            method:'post',
            success: function(res){
               console.log(res)
                if(res.data.success){
                    wx.showToast({
                      title: '收藏成功',
                    })
                    that.getCollection(that.data.obj.id)
                }else{
                    wx.showToast({
                        title: '你已经收藏了。',
                      })
                that.getCollection(that.data.obj.id)
                }
            }
        })
    },
    handleCartCancel(){
        let that = this
        let obj = {
            opcode: "Int16Array", 
            id:that.data.collectionId
        }
        api.collectiondel({
            data:obj,
            method:'post',
            success: function(res){
               console.log(res)
                if(res.data.success){
                    wx.showToast({
                      title: '取消成功',
                    })
                    that.getCollection(that.data.obj.id)
                }else{
                    wx.showToast({
                        title: '取消失败',
                      })
                      that.getCollection(that.data.obj.id)
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
})